package bgate.hung.workerclass;

import java.io.IOException;
import java.util.Vector;
import bgate.font.PiPoDesigner;
import bgate.hung.activity.PhomActivity;
import bgate.hung.activity.R;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.inputmethodservice.ExtractEditText;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

public class PhomView extends SurfaceView implements SurfaceHolder.Callback {
	public class GameThread extends Thread {
		public static final int MAX_ACC_LENGTH = 10;
		public static final int STATE_LOSE = 1;
		public static final int STATE_PAUSE = 2;
		public static final int STATE_READY = 3;
		public static final int STATE_RUNNING = 4;
		public static final int STATE_WIN = 5;
		private static final int MAX_CPS = 70;
		private static final int MS_PER_FRAME = 1000 / MAX_CPS;
		private static final int THUONG = 1;
		private static final int CHOT = 2;
		private static final int UPDATE_BUTTON0 = 0;
		private static final int UPDATE_BUTTON1 = 1;
		private static final int UPDATE_BUTTON2 = 2;
		private static final int UPDATE_BUTTON3 = 3;
		private static final int MAX_LENGTH_CHAT_TEXT = 20;
		private static final int MAX_LENGTH_MONEY_EFFECT = 50;

		private Resources res;
		private boolean mRun = false;
		private int mMode = STATE_PAUSE;
		private SurfaceHolder mSurfaceHolder;
		private Context mContext;
		private Bitmap background, avatar, blackAvatar, logo;
		// private Bitmap chat_middle;
		// private Bitmap chat_header_right;
		// private Bitmap chat_header_left;
		// private Bitmap chat_footer_right;
		// private Bitmap chat_footer_left;
		private int mCanvasWidth, mCanvasHeight;
		private int d6h, d6h_phom, d6h_removed;
		private int d12h, d12h_phom, d12h_removed;
		private int d3h, d3h_phom, d3h_removed;
		private int d9h, d9h_phom, d9h_removed;
		private int seqCard = 0;
		private int cardWidth, cardHeight;
		private Vector<Card> card6h = new Vector<Card>();
		private Vector<Card> tempCard = new Vector<Card>();
		private Vector<Card> card6hSaved = new Vector<Card>();
		private Vector<Card> card6h_phom = new Vector<Card>();
		private Vector<Card> removedCard6h = new Vector<Card>();
		private Vector<Card> card3h = new Vector<Card>();
		private Vector<Card> card3h_phom = new Vector<Card>();
		private Vector<Card> removedCard3h = new Vector<Card>();
		private Vector<Card> card12h = new Vector<Card>();
		private Vector<Card> card12h_phom = new Vector<Card>();
		private Vector<Card> removedCard12h = new Vector<Card>();
		private Vector<Card> card9h = new Vector<Card>();
		private Vector<Card> card9h_phom = new Vector<Card>();
		private Vector<Card> removedCard9h = new Vector<Card>();
		private Vector<Card> choiseCard = new Vector<Card>();
		private Vector<Card> cardAn = new Vector<Card>();
		private Vector<Integer> db_data = new Vector<Integer>();
		private Vector<Integer> playerActivted = new Vector<Integer>();
		private Card movedCard;
		private int XBegin6h, XBegin6h_phom, XBegin6h_removed;
		private int YBegin3h, YBegin3h_phom, YBegin3h_removed;
		private int XBegin12h, XBegin12h_phom, XBegin12h_removed;
		private int YBegin9h, YBegin9h_phom, YBegin9h_removed;
		private int arX, arY, deltaY, scale;
		private String acc = "";
		private Player[] players = new Player[4];
		private int playerStart;
		private int boss;
		private int x6h, y6h;
		private int x3h, y3h;
		private int x12h, y12h;
		private int x9h, y9h;
		private boolean startToast = false;
		private Bitmap an_button_image, danh_button_image, ha_button_image,
				chat_button_image, ready_button_image, not_ready_button_image,
				boc_button_image, nap_ycoin_image, continue_button_image,
				time_bar_zero_vertical, time_bar_full_vertical,
				time_bar_zero_horizontal, time_bar_full_horizontal;
		private PlayButton an_button, danh_button, ha_button, chat_button,
				boc_button, nap_ycoin_button, continue_button;
		private ReadyButton readyButton;
		private int stt_db;
		private int timeForToast = 2000;
		private long beginToast = 0l;
		private int luotDanh = 0;
		private boolean display_ha_phom = false;
		private boolean display_danh = false;
		private boolean display_an = false;
		private boolean display_boc = false;
		private boolean isDrap = false;
		private boolean isTouch = false;
		private boolean touchFlag = true;
		private boolean isPlay = false;
		private int initX;
		private int initY;
		private long lastTime = 0l;
		private int time = 30;
		private Paint paint;
		private Paint textPaint1;
		private Paint textPaint2;
		private boolean drawClock = false;
		private boolean isContinue = false;
		private boolean checkU = true;
		private boolean denLang = false;
		private boolean hasU = false;
		private boolean start_ha_phom = false;
		private Player[] ketqua;
		private boolean moving = false;
		private boolean chia_bai = false;
		private boolean chia = false;
		private int dem_chia = 0;
		private Vector<Card> source;
		private Vector<Card> target;
		private int numberCard = 0;
		private int so_bai_truoc_mat = 0;
		private int update_button;
		private boolean an_bai_6h = false;
		private boolean chat = false;
		// private boolean showEditText = false;
		// private InputMethodManager input;
		private String chatMessage = "";
		private int stt_chat = 0;
		private int time_for_chat = 5000;
		private long last_chat = 0l;
		private long last_effect = 0l;
		private boolean clear = false;
		private int d = 0;
		private boolean draw_money_effect = false;
		private Rect dst, src, horizonDstRect, horizonSrcRect, verticalDstRect,
				verticalSrcRect;
		private Bitmap frame;
		private Bitmap frame1;
		private int YBeginFrame;

		public GameThread(SurfaceHolder surfaceHolder, Context context) {
			mSurfaceHolder = surfaceHolder;
			mContext = context;
			this.mCanvasWidth = StaticData.WIDTH;
			this.mCanvasHeight = StaticData.HEIGHT;

			res = context.getResources();
			initialize();
		}

		/**
		 * Initialize the game.
		 */
		public void initialize() {
			scale = StaticData.WIDTH / 480;

			frame = BitmapFactory.decodeResource(res, R.drawable.frame);
			frame1 = BitmapFactory.decodeResource(res, R.drawable.frame1);

			background = StaticBitmap.getPlayBackGroundImage();
			dst = new Rect(0, 0, mCanvasWidth, mCanvasHeight);
			src = new Rect(0, 0, background.getWidth(), background.getHeight());
			avatar = StaticBitmap.getBoyAvatarImage();
			blackAvatar = StaticBitmap.getBoyBlackAvatarImage();
			logo = StaticBitmap.getLogoPhomImage();

			an_button_image = StaticBitmap.getAnButtonImage();
			danh_button_image = StaticBitmap.getDanhButtonImage();
			ha_button_image = StaticBitmap.getHaButtonImage();
			chat_button_image = StaticBitmap.getChatButtonImage();
			ready_button_image = StaticBitmap.getReadyButtonImage();
			not_ready_button_image = StaticBitmap.getNotReadyButtonImage();
			boc_button_image = StaticBitmap.getBocButtonImage();
			nap_ycoin_image = StaticBitmap.getNapYcoinButtonImage();
			continue_button_image = StaticBitmap.getContinueButtonImage();
			time_bar_full_horizontal = StaticBitmap.getTimeBarFullHorizontal();
			time_bar_full_vertical = StaticBitmap.getTimeBarFullVertical();
			time_bar_zero_horizontal = StaticBitmap.getTimeBarZeroHorizontal();
			time_bar_zero_vertical = StaticBitmap.getTimeBarZeroVertical();

			horizonDstRect = new Rect(0, 0,
					time_bar_full_horizontal.getWidth(),
					time_bar_full_horizontal.getHeight());
			horizonSrcRect = new Rect(0, 0,
					time_bar_full_horizontal.getWidth(),
					time_bar_full_horizontal.getHeight());
			verticalDstRect = new Rect(0, 0, time_bar_full_vertical.getWidth(),
					time_bar_full_vertical.getHeight());
			verticalSrcRect = new Rect(0, 0, time_bar_full_vertical.getWidth(),
					time_bar_full_vertical.getHeight());

			readyButton = new ReadyButton(ready_button_image,
					not_ready_button_image, mCanvasWidth
							- ready_button_image.getWidth() - 10, mCanvasHeight
							- ready_button_image.getHeight() - 5);
			// chat_middle = BitmapFactory.decodeResource(res,
			// R.drawable.chat_middle);
			// chat_header_left = BitmapFactory.decodeResource(res,
			// R.drawable.chat_header_1);
			// chat_header_right = BitmapFactory.decodeResource(res,
			// R.drawable.chat_header_2);
			// chat_footer_left = BitmapFactory.decodeResource(res,
			// R.drawable.chat_footer_1);
			// chat_footer_right = BitmapFactory.decodeResource(res,
			// R.drawable.chat_footer_2);

			chat_button = new PlayButton(chat_button_image, 5, mCanvasHeight
					- chat_button_image.getHeight() - 5);
			an_button = new PlayButton(an_button_image, chat_button.getX()
					+ chat_button.getWidth() + 10, mCanvasHeight
					- an_button_image.getHeight() - 5);
			boc_button = new PlayButton(boc_button_image, mCanvasWidth
					- boc_button_image.getWidth() - 10, mCanvasHeight
					- boc_button_image.getHeight() - 5);
			danh_button = new PlayButton(danh_button_image, boc_button.getX()
					- danh_button_image.getWidth() - 10, boc_button.getY());
			ha_button = new PlayButton(ha_button_image, mCanvasWidth
					- ha_button_image.getWidth() - 10, mCanvasHeight
					- ha_button_image.getHeight() - 5);
			nap_ycoin_button = new PlayButton(nap_ycoin_image, mCanvasWidth / 2
					- nap_ycoin_image.getWidth() / 2, mCanvasHeight
					- nap_ycoin_image.getHeight() - 5);
			continue_button = new PlayButton(continue_button_image,
					mCanvasWidth - continue_button_image.getWidth() - 10,
					mCanvasHeight - continue_button_image.getHeight() - 5);

			cardHeight = StaticBitmap.getCardImage().getHeight();
			cardWidth = StaticBitmap.getCardImage().getWidth() / 53;

			paint = new Paint();
			paint.setTextSize(20);
			paint.setTypeface(Typeface.create("Arial", Typeface.BOLD));
			paint.setColor(Color.WHITE);

			textPaint1 = new Paint();
			textPaint1.setTextSize(15);
			textPaint1.setTypeface(Typeface.create("Arial", Typeface.BOLD));
			textPaint1.setColor(Color.WHITE);

			textPaint2 = new Paint();
			textPaint2.setTextSize(15);
			textPaint2.setTypeface(Typeface.create("Arial", Typeface.BOLD));
			textPaint2.setColor(Color.RED);

			PiPoDesigner.init(null, null, null, null, null, null);

			d6h = 2 * cardWidth / 3;
			d6h_removed = cardWidth / 3;
			d6h_phom = cardWidth / 5;
			d3h = cardHeight / 3;
			d3h_phom = cardHeight / 5;
			d3h_removed = cardHeight / 5;
			d12h = cardWidth / 2;
			d12h_phom = cardWidth / 5;
			d12h_removed = cardWidth / 3;
			d9h = d3h;
			d9h_phom = d3h_phom;
			d9h_removed = d3h_removed;

			arX = 10 * scale;
			arY = readyButton.getY() - 5 * scale;
			deltaY = mCanvasHeight - readyButton.getY() + 5 * scale;

			x6h = mCanvasWidth / 2 - avatar.getWidth() / 2;
			y6h = mCanvasHeight - ready_button_image.getHeight() - 10
					- avatar.getHeight();

			x3h = mCanvasWidth - avatar.getWidth() - 10;
			y3h = mCanvasHeight / 2 - ready_button_image.getHeight() - 10
					- avatar.getHeight() / 2;

			x12h = mCanvasWidth / 2 - avatar.getWidth() / 2;
			y12h = 10;

			x9h = 10;
			y9h = mCanvasHeight / 2 - ready_button_image.getHeight() - 10
					- avatar.getHeight() / 2;

			doStart();
		}

		private void initAgain() {
			for (int i = 0; i < 4; i++) {
				if (players[i].isMe) {
					players[i].setX(x6h);
					players[i].setY(y6h);
					if (players[i].getState() != Player.BOSS)
						players[i].setState(Player.WAITTING);

					int i2 = 0;
					if (i + 1 < 4)
						i2 = i + 1;
					else
						i2 = 0;
					players[i2].setX(x3h);
					players[i2].setY(y3h);
					if (players[i2].getState() != Player.BOSS)
						players[i2].setState(Player.WAITTING);

					int i3 = 0;
					if (i2 + 1 < 4)
						i3 = i2 + 1;
					else
						i3 = 0;
					players[i3].setX(x12h);
					players[i3].setY(y12h);
					if (players[i3].getState() != Player.BOSS)
						players[i3].setState(Player.WAITTING);

					int i4 = 0;
					if (i3 + 1 < 4)
						i4 = i3 + 1;
					else
						i4 = 0;
					players[i4].setX(x9h);
					players[i4].setY(y9h);
					if (players[i4].getState() != Player.BOSS)
						players[i4].setState(Player.WAITTING);
				}
			}

			for (int i = 0; i < players.length; i++)
				players[i].reset();
			if (card6h.size() > 0)
				card6h.removeAllElements();
			if (card6hSaved.size() > 0)
				card6hSaved.removeAllElements();
			if (card6h_phom.size() > 0)
				card6h_phom.removeAllElements();
			if (removedCard6h.size() > 0)
				removedCard6h.removeAllElements();
			if (card3h.size() > 0)
				card3h.removeAllElements();
			if (card3h_phom.size() > 0)
				card3h_phom.removeAllElements();
			if (removedCard3h.size() > 0)
				removedCard3h.removeAllElements();
			if (card12h.size() > 0)
				card12h.removeAllElements();
			if (card12h_phom.size() > 0)
				card12h_phom.removeAllElements();
			if (removedCard12h.size() > 0)
				removedCard12h.removeAllElements();
			if (card9h.size() > 0)
				card9h.removeAllElements();
			if (card9h_phom.size() > 0)
				card9h_phom.removeAllElements();
			if (removedCard9h.size() > 0)
				removedCard9h.removeAllElements();
			for (int i = 0; i < players.length; i++)
				players[i].drawState = true;
			if (playerActivted.size() > 0)
				playerActivted.removeAllElements();
			if (cardAn.size() > 0) {
				cardAn.removeAllElements();
			}
			if (choiseCard.size() > 0)
				choiseCard.removeAllElements();
			if (db_data.size() > 0)
				db_data.removeAllElements();
			if (tempCard.size() > 0)
				tempCard.removeAllElements();
			display_ha_phom = false;
			display_danh = false;
			display_an = false;
			display_boc = false;
			startToast = false;
			isDrap = false;
			isContinue = false;
			checkU = true;
			denLang = false;
			hasU = false;
			chia_bai = false;
			start_ha_phom = false;
			an_bai_6h = false;
			chat = false;
			draw_money_effect = false;
			// showEditText = false;
			d = 0;
			time = 30;
			readyButton.setBitmap(ready_button_image);
			luotDanh = 0;
			numberCard = 0;
			chia = false;
			clear = false;
			dem_chia = 0;
			update_button = 0;
			so_bai_truoc_mat = 0;
			chatMessage = "";
			stt_chat = 0;
			last_chat = 0l;
			last_effect = 0l;
		}

		public void setTableData(String[] ro_arr, int[] mo_arr, int[] re_arr,
				boolean set_ro, boolean set_mo, boolean set_re) {

			boss = Integer.valueOf(ro_arr[5]);
			// re_arr[boss] = Player.BOSS;
			playerStart = Integer.valueOf(ro_arr[6]);
			if (ro_arr.length > 0)
				playerActivted.removeAllElements();

			if (players[0] == null)
				players[0] = new Player(ro_arr[1], mo_arr[0], re_arr[0]);
			else {
				if (set_ro)
					players[0].setAccount(ro_arr[1]);
				if (set_mo)
					players[0].setMoney(mo_arr[0]);
				if (set_re)
					players[0].setState(re_arr[0]);
			}
			if (ro_arr[1].equals("~"))
				players[0].isHiden = true;
			else {
				players[0].isHiden = false;
				playerActivted.addElement(0);
			}

			if (players[1] == null)
				players[1] = new Player(ro_arr[2], mo_arr[1], re_arr[1]);
			else {
				if (set_ro)
					players[1].setAccount(ro_arr[2]);
				if (set_mo)
					players[1].setMoney(mo_arr[1]);
				if (set_re)
					players[1].setState(re_arr[1]);
			}
			if (ro_arr[2].equals("~"))
				players[1].isHiden = true;
			else {
				players[1].isHiden = false;
				playerActivted.addElement(1);
			}

			if (players[2] == null)
				players[2] = new Player(ro_arr[3], mo_arr[2], re_arr[2]);
			else {
				if (set_ro)
					players[2].setAccount(ro_arr[3]);
				if (set_mo)
					players[2].setMoney(mo_arr[2]);
				if (set_re)
					players[2].setState(re_arr[2]);
			}
			if (ro_arr[3].equals("~"))
				players[2].isHiden = true;
			else {
				players[2].isHiden = false;
				playerActivted.addElement(2);
			}

			if (players[3] == null)
				players[3] = new Player(ro_arr[4], mo_arr[3], re_arr[3]);
			else {
				if (set_ro)
					players[3].setAccount(ro_arr[4]);
				if (set_mo)
					players[3].setMoney(mo_arr[3]);
				if (set_re)
					players[3].setState(re_arr[3]);
			}
			if (ro_arr[4].equals("~"))
				players[3].isHiden = true;
			else {
				players[3].isHiden = false;
				playerActivted.addElement(3);
			}

			players[boss].setState(Player.BOSS);

			if (ro_arr[1].equals(acc)) {
				players[0].isMe = true;
				players[1].isMe = false;
				players[2].isMe = false;
				players[3].isMe = false;
			} else if (ro_arr[2].equals(acc)) {
				players[0].isMe = false;
				players[1].isMe = true;
				players[2].isMe = false;
				players[3].isMe = false;
			} else if (ro_arr[3].equals(acc)) {
				players[0].isMe = false;
				players[1].isMe = false;
				players[2].isMe = true;
				players[3].isMe = false;
			} else if (ro_arr[4].equals(acc)) {
				players[0].isMe = false;
				players[1].isMe = false;
				players[2].isMe = false;
				players[3].isMe = true;
			}

			if (!isPlay)
				for (int i = 0; i < 4; i++) {
					if (players[i].isMe) {
						players[i].setX(x6h);
						players[i].setY(y6h);
						players[i].setPositon(Player.POSITION_6H);

						int i2 = 0;
						if (i + 1 < 4)
							i2 = i + 1;
						else
							i2 = 0;
						players[i2].setX(x3h);
						players[i2].setY(y3h);
						players[i2].setPositon(Player.POSITION_3H);

						int i3 = 0;
						if (i2 + 1 < 4)
							i3 = i2 + 1;
						else
							i3 = 0;
						players[i3].setX(x12h);
						players[i3].setY(y12h);
						players[i3].setPositon(Player.POSITION_12H);

						int i4 = 0;
						if (i3 + 1 < 4)
							i4 = i3 + 1;
						else
							i4 = 0;
						players[i4].setX(x9h);
						players[i4].setY(y9h);
						players[i4].setPositon(Player.POSITION_9H);
					}
				}

			for (int i = 0; i < playerActivted.size(); i++) {
				if (playerStart == playerActivted.elementAt(i))
					luotDanh = i;
			}

			if (re_arr[getSttMe()] == 0) {
				readyButton.setBitmap(ready_button_image);
			} else {
				readyButton.setBitmap(not_ready_button_image);
			}

			int countHiden = 0;
			for (int i = 0; i < players.length; i++)
				if (players[i].isHiden)
					countHiden++;
			if (isPlay && countHiden == 3) {
				players[getSttMe()].setKetQua(Player.THANG);
				ketqua = sortKetQua();
				isContinue = true;
			}
		}

		public void setPlayerStart(int t) {
			playerStart = t;
			for (int i = 0; i < playerActivted.size(); i++) {
				if (playerStart == playerActivted.elementAt(i))
					luotDanh = i;
			}
		}

		public void setUserInfor(String s) {
			acc = s;
		}

		public void setSCVector(Vector<Integer> v1, Vector<Integer> v2) {
			v1 = sortIntOfCard(v1);

			for (int i = 0; i < v1.size(); i++) {
				Card card = new Card(mCanvasWidth / 2 - cardWidth / 2,
						mCanvasHeight / 2 - cardHeight / 2, MS_PER_FRAME, 53,
						v1.elementAt(i));
				card.setFrame(52);
				tempCard.addElement(card);
			}

			source = tempCard;
			target = card6h;
			numberCard = tempCard.size();

			for (int i = 0; i < players.length; i++) {
				players[i].setMoney(v2.elementAt(i));
				players[i].drawState = false;
			}

			for (int i = 0; i < 4; i++) {
				if (players[i].isMe) {
					players[i].setPositon(Player.POSITION_6H);

					int i2 = 0;
					if (i + 1 < 4)
						i2 = i + 1;
					else
						i2 = 0;
					players[i2].setPositon(Player.POSITION_3H);

					int i3 = 0;
					if (i2 + 1 < 4)
						i3 = i2 + 1;
					else
						i3 = 0;
					players[i3].setPositon(Player.POSITION_12H);

					int i4 = 0;
					if (i3 + 1 < 4)
						i4 = i3 + 1;
					else
						i4 = 0;
					players[i4].setPositon(Player.POSITION_9H);
				}
			}
			chia_bai = true;
			isPlay = true;
			chia = true;
		}

		public void setDBData(Vector<Integer> v) {
			int i = v.elementAt(0);
			db_data = v;
			if (players[i].getPosition() == Player.POSITION_12H) {
				XBegin12h_removed = (mCanvasWidth - removedCard12h.size()
						* d12h_removed - cardWidth) / 2;
				Card card = new Card(mCanvasWidth / 2 - cardWidth / 2,
						0 - cardHeight, MS_PER_FRAME, 53, v.elementAt(1));
				card.setDist(XBegin12h_removed + removedCard12h.size()
						* d12h_removed, 30 * mCanvasHeight / 320);
				card.state = Card.MOVING;
				tempCard.removeAllElements();
				tempCard.addElement(card);
				source = tempCard;
				target = removedCard12h;
				moving = true;
				setPositionRemovedCard12h(removedCard12h.size() + 1);
			}

			else if (players[i].getPosition() == Player.POSITION_9H) {
				YBegin9h_removed = (mCanvasHeight - removedCard9h.size()
						* d9h_removed - cardHeight)
						/ 2 - deltaY;
				Card card = new Card(0 - cardWidth, mCanvasHeight / 2
						- cardHeight / 2, MS_PER_FRAME, 53, v.elementAt(1));
				card.setDist(45 * mCanvasWidth / 480, YBegin9h_removed
						+ removedCard9h.size() * d9h_removed);
				card.state = Card.MOVING;
				tempCard.removeAllElements();
				tempCard.addElement(card);
				source = tempCard;
				target = removedCard9h;
				moving = true;
				setPositionRemovedCard9h(removedCard9h.size() + 1);
			} else if (players[i].getPosition() == Player.POSITION_3H) {
				YBegin3h_removed = (mCanvasHeight - removedCard3h.size()
						* d3h_removed - cardHeight)
						/ 2 - deltaY;
				Card card = new Card(mCanvasWidth + cardWidth, mCanvasHeight
						/ 2 - cardHeight / 2, MS_PER_FRAME, 53, v.elementAt(1));
				card.setDist(385 * mCanvasWidth / 480, YBegin3h_removed
						+ removedCard3h.size() * d3h_removed);
				card.state = Card.MOVING;
				tempCard.removeAllElements();
				tempCard.addElement(card);
				source = tempCard;
				target = removedCard3h;
				moving = true;
				setPositionRemovedCard3h(removedCard3h.size() + 1);
			}

			if (so_bai_truoc_mat == 4) {
				Vector<Integer> temp = new Vector<Integer>();
				for (int j = 0; j < card6h.size(); j++)
					temp.addElement(card6h.elementAt(j).getId());
				drawClock = false;
				((PhomActivity) mContext).sendSCOData(temp);
			}

			nextLuotDanhBai();
			setTime();
			startClock(true);
		}

		public void setBCData(int b) {
			Card card = new Card(mCanvasWidth / 2 - cardWidth / 2,
					mCanvasHeight / 2 - cardHeight / 2, MS_PER_FRAME, 53, b);
			card.setFrame(52);

			if (chia_bai) {
				XBegin6h = (mCanvasWidth - (10 - 1) * d6h - cardWidth) / 2
						+ arX;
				card.setDist(XBegin6h + 9 * d6h, arY - cardHeight);
				tempCard.add(tempCard.size() - 1, card);
			} else {
				XBegin6h = (mCanvasWidth - card6h.size() * d6h - cardWidth) / 2
						+ arX;
				card.setDist(XBegin6h + card6h.size() * d6h, arY - cardHeight);
				for (int i = 0; i < card6h.size(); i++) {
					card6h.elementAt(i).setX(XBegin6h + d6h * i);
					card6h.elementAt(i).setY(arY - cardHeight);
				}
				card.state = Card.MOVING;
				tempCard.addElement(card);
			}
			source = tempCard;
			target = card6h;
			moving = true;
		}

		public void setEAData(Vector<Integer> ea_vector) {
			int stt_bi_an = ea_vector.elementAt(0);
			int stt_an = getBehindPlayer(stt_bi_an);
			int id = ea_vector.elementAt(1);

			if (players[stt_an].getPosition() == Player.POSITION_12H) {
				XBegin12h = (mCanvasWidth - card12h.size() * d12h - cardWidth) / 2;
				Card temp = new Card(id);
				Card card_bi_an = getRemovedCardOfPlayer(stt_bi_an).elementAt(
						getCardPositionInVector(temp,
								getRemovedCardOfPlayer(stt_bi_an)));
				card_bi_an.setDist(XBegin12h + card12h.size() * d12h,
						5 * mCanvasHeight / 320);
				card_bi_an.state = Card.MOVING;
				tempCard.removeAllElements();
				tempCard.addElement(card_bi_an);
				source = tempCard;
				target = card12h;
				moving = true;
				setPositionCard12h(card12h.size() + 1, d12h);
			} else if (players[stt_an].getPosition() == Player.POSITION_9H) {
				YBegin9h = (mCanvasHeight - card9h.size() * d9h - cardHeight)
						/ 2 - deltaY;
				Card temp = new Card(id);
				Card card_bi_an = getRemovedCardOfPlayer(stt_bi_an).elementAt(
						getCardPositionInVector(temp,
								getRemovedCardOfPlayer(stt_bi_an)));
				card_bi_an.setDist(10 * mCanvasWidth / 480,
						YBegin9h + card9h.size() * d9h);
				card_bi_an.state = Card.MOVING;
				tempCard.removeAllElements();
				tempCard.addElement(card_bi_an);
				source = tempCard;
				target = card9h;
				moving = true;
				setPositionCard9h(card9h.size() + 1, d9h);
			} else if (players[stt_an].getPosition() == Player.POSITION_3H) {
				YBegin3h = (mCanvasHeight - card3h.size() * d3h - cardHeight)
						/ 2 - deltaY;
				Card temp = new Card(id);
				Card card_bi_an = getRemovedCardOfPlayer(stt_bi_an).elementAt(
						getCardPositionInVector(temp,
								getRemovedCardOfPlayer(stt_bi_an)));
				card_bi_an.setDist(420 * mCanvasWidth / 480,
						YBegin3h + card3h.size() * d3h);
				card_bi_an.state = Card.MOVING;
				tempCard.removeAllElements();
				tempCard.addElement(card_bi_an);
				source = tempCard;
				target = card3h;
				moving = true;
				setPositionCard3h(card3h.size() + 1, d3h);
			} else if (players[stt_an].getPosition() == Player.POSITION_6H) {
				XBegin6h = (mCanvasWidth - (card6h.size() - 1) * d6h - cardWidth)
						/ 2 + arX;
				Card temp = getRemovedCardOfPlayer(stt_bi_an).elementAt(
						getCardPositionInVector(new Card(id),
								getRemovedCardOfPlayer(stt_bi_an)));
				Card card_bi_an = new Card(temp.getX(), temp.getY(),
						MS_PER_FRAME, 53, id);
				card_bi_an.setDist(XBegin6h, arY - cardHeight);
				card_bi_an.state = Card.MOVING;
				tempCard.removeAllElements();
				tempCard.addElement(card_bi_an);
				source = tempCard;
				target = card6h;
				an_bai_6h = true;
				moving = true;
				Vector<Card> temp_vector = new Vector<Card>();
				for (int i = 0; i < choiseCard.size(); i++) {
					temp_vector.addElement(choiseCard.elementAt(i));
				}
				temp_vector.addElement(new Card(0, 0, MS_PER_FRAME, 53, id));
				temp_vector = sortCard(temp_vector);
				for (int i = 0; i < card6h.size(); i++) {
					for (int j = 0; j < choiseCard.size(); j++) {
						if (choiseCard.elementAt(j).getId() == card6h
								.elementAt(i).getId())
							card6h.remove(i);
					}
				}
				for (int i = temp_vector.size() - 1; i >= 0; i--)
					card6h.insertElementAt(temp_vector.elementAt(i), 0);

				if (stt_an == getSttMe()) {
					System.out.println("TOI AN CUA: " + stt_bi_an);
					players[getSttMe()].setAnBai(stt_bi_an);
				}

				setPositionCard6h();

				cardAn.addElement(new Card(id));
			}

			getRemovedCardOfPlayer(stt_bi_an).removeElementAt(
					getRemovedCardOfPlayer(stt_bi_an).size() - 1);

			// Chuyen ha dau
			int vt = stt_an;
			for (int i = 0; i < 3; i++) {
				if (getRemovedCardOfPlayer(vt).size() > getRemovedCardOfPlayer(
						stt_bi_an).size()) {
					int number = getRemovedCardOfPlayer(vt).size();
					Card temp = getRemovedCardOfPlayer(vt)
							.elementAt(number - 1);
					getRemovedCardOfPlayer(stt_bi_an).addElement(
							new Card(0, 0, MS_PER_FRAME, 53, temp.getId()));
					getRemovedCardOfPlayer(vt).removeElementAt(number - 1);
					break;
				} else {
					vt++;
					if (vt > 3)
						vt = 0;
				}
			}

			if (removedCard6h.size() < 3) {
				if (display_ha_phom) {
					display_ha_phom = false;
					display_danh = true;
				}
			}

			// Set lai toa do cac quan bai
			if (players[stt_bi_an].getPosition() == Player.POSITION_12H) {
				setPositionRemovedCard12h(removedCard12h.size());
			} else if (players[stt_bi_an].getPosition() == Player.POSITION_9H) {
				setPositionRemovedCard9h(removedCard9h.size());
			} else if (players[stt_bi_an].getPosition() == Player.POSITION_6H) {
				setPositionRemovedCard6h(removedCard6h.size());
			} else {
				setPositionRemovedCard3h(removedCard3h.size());
			}

		}

		public void setCMData(Vector<Integer> v1, Vector<Integer> v2) {
			int[] id_cm = new int[v1.size()];
			int[] money_cm = new int[v2.size()];

			for (int i = 0; i < v1.size(); i++)
				id_cm[i] = v1.elementAt(i);
			for (int i = 0; i < v2.size(); i++)
				money_cm[i] = v2.elementAt(i);

			for (int i = 0; i < players.length; i++) {
				for (int j = 0; j < id_cm.length; j++) {
					if (i == id_cm[j]) {
						players[i]
								.setMoney(players[i].getMoney() + money_cm[j]);
						players[i].setMoneyEffect(money_cm[j]);
						players[i].draw_effect = true;
					}
				}
			}
			draw_money_effect = true;
		}

		public void setHPData(Vector<Integer> v) {
			int vt = v.elementAt(0);

			if (players[vt].getPosition() == Player.POSITION_12H) {
				card12h_phom.removeAllElements();
				for (int i = 1; i < v.size(); i++) {
					card12h_phom.addElement(new Card(0, 0, MS_PER_FRAME, 53, v
							.elementAt(i)));
				}
				setPositionPhom12h();
				card12h.removeAllElements();
			} else if (players[vt].getPosition() == Player.POSITION_3H) {
				card3h_phom.removeAllElements();
				for (int i = 1; i < v.size(); i++) {
					card3h_phom.addElement(new Card(0, 0, MS_PER_FRAME, 53, v
							.elementAt(i)));
				}
				setPositionPhom3h();
				card3h.removeAllElements();
			} else if (players[vt].getPosition() == Player.POSITION_9H) {
				card9h_phom.removeAllElements();
				for (int i = 1; i < v.size(); i++) {
					card9h_phom.addElement(new Card(0, 0, MS_PER_FRAME, 53, v
							.elementAt(i)));
				}
				setPositionPhom9h();
				card9h.removeAllElements();
			}
		}

		public void setFNData(Vector<Integer> v0, Vector<Integer> v1,
				Vector<Integer> v2, Vector<Integer> v3) {

			card12h.removeAllElements();
			card9h.removeAllElements();
			card3h.removeAllElements();

			if (v0.size() > 0) {
				players[0].setKetQua(v0.elementAt(0));
				if (players[0].getPosition() == Player.POSITION_12H) {
					for (int i = 1; i < v0.size(); i++) {
						card12h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v0
								.elementAt(i)));
					}
					setPositionCard12h(card12h.size(), d12h_phom);
				} else if (players[0].getPosition() == Player.POSITION_3H) {
					for (int i = 1; i < v0.size(); i++) {
						card3h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v0
								.elementAt(i)));
					}
					setPositionCard3h(card3h.size(), d3h_phom);
				} else if (players[0].getPosition() == Player.POSITION_9H) {
					for (int i = 1; i < v0.size(); i++) {
						card9h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v0
								.elementAt(i)));
					}
					setPositionCard9h(card9h.size(), d9h_phom);
				}
			}

			if (v1.size() > 0) {
				players[1].setKetQua(v1.elementAt(0));
				if (players[1].getPosition() == Player.POSITION_12H) {
					for (int i = 1; i < v1.size(); i++) {
						card12h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v1
								.elementAt(i)));
					}
					setPositionCard12h(card12h.size(), d12h_phom);
				} else if (players[1].getPosition() == Player.POSITION_3H) {
					for (int i = 1; i < v1.size(); i++) {
						card3h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v1
								.elementAt(i)));
					}
					setPositionCard3h(card3h.size(), d3h_phom);
				} else if (players[1].getPosition() == Player.POSITION_9H) {
					for (int i = 1; i < v1.size(); i++) {
						card9h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v1
								.elementAt(i)));
					}
					setPositionCard9h(card9h.size(), d9h_phom);
				}
			}

			if (v2.size() > 0) {
				players[2].setKetQua(v2.elementAt(0));
				if (players[2].getPosition() == Player.POSITION_12H) {
					for (int i = 1; i < v2.size(); i++) {
						card12h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v2
								.elementAt(i)));
					}
					setPositionCard12h(card12h.size(), d12h_phom);
				} else if (players[2].getPosition() == Player.POSITION_3H) {
					for (int i = 1; i < v2.size(); i++) {
						card3h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v2
								.elementAt(i)));
					}
					setPositionCard3h(card3h.size(), d3h_phom);
				} else if (players[2].getPosition() == Player.POSITION_9H) {
					for (int i = 1; i < v2.size(); i++) {
						card9h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v2
								.elementAt(i)));
					}
					setPositionCard9h(card9h.size(), d9h_phom);
				}
			}

			if (v3.size() > 0) {
				players[3].setKetQua(v3.elementAt(0));
				if (players[3].getPosition() == Player.POSITION_12H) {
					for (int i = 1; i < v3.size(); i++) {
						card12h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v3
								.elementAt(i)));
					}
					setPositionCard12h(card12h.size(), d12h_phom);
				} else if (players[3].getPosition() == Player.POSITION_3H) {
					for (int i = 1; i < v3.size(); i++) {
						card3h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v3
								.elementAt(i)));
					}
					setPositionCard3h(card3h.size(), d3h_phom);
				} else if (players[3].getPosition() == Player.POSITION_9H) {
					for (int i = 1; i < v3.size(); i++) {
						card9h.addElement(new Card(0, 0, MS_PER_FRAME, 53, v3
								.elementAt(i)));
					}
					setPositionCard9h(card9h.size(), d9h_phom);
				}
			}

			ketqua = sortKetQua();
			isContinue = true;
			display_danh = false;
		}

		public void setUData(int u) {
			isContinue = true;
			ketqua = new Player[playerActivted.size()];
			for (int i = 0; i < playerActivted.size(); i++) {
				if (playerActivted.elementAt(i) == u) {
					players[u].setKetQua(Player.U);
					ketqua[0] = players[u];
					int count = 1;
					for (int j = 0; j < playerActivted.size(); j++) {
						if (playerActivted.elementAt(j) != u) {
							ketqua[count] = players[playerActivted.elementAt(j)];
							count++;
						}

					}
					break;
				}
			}
		}

		public void setDLData(int user, int preUser) {
			isContinue = true;
			ketqua = new Player[playerActivted.size()];
			for (int i = 0; i < playerActivted.size(); i++) {
				if (playerActivted.elementAt(i) == user) {
					players[user].setKetQua(Player.U_DEN);
					ketqua[0] = players[user];
					break;
				}
			}
			for (int i = 0; i < playerActivted.size(); i++) {
				if (playerActivted.elementAt(i) == preUser) {
					players[preUser].setKetQua(Player.DEN_LANG);
					ketqua[1] = players[preUser];
					break;
				}
			}
		}

		public void setChatData(int i, String s) {
			chat = true;
			last_chat = System.currentTimeMillis();
			chatMessage = s;
			stt_chat = i;
		}

		public void startClock(boolean b) {
			drawClock = b;
		}

		/**
		 * Starts the game.
		 */
		public void doStart() {
			synchronized (mSurfaceHolder) {

			}

			setState(STATE_RUNNING);
		}

		public void pause() {
			synchronized (mSurfaceHolder) {
				if (mMode == STATE_RUNNING)
					setState(STATE_PAUSE);
			}
		}

		public synchronized void restoreState(Bundle savedState) {
			synchronized (mSurfaceHolder) {

			}
		}

		@Override
		public void run() {
			while (mRun) {
				Canvas c = null;
				try {
					c = mSurfaceHolder.lockCanvas(null);
					synchronized (mSurfaceHolder) {
						if (mMode == STATE_RUNNING)
							updatePhysics();
						doDraw(c);
					}
				} finally {
					if (c != null) {
						mSurfaceHolder.unlockCanvasAndPost(c);
					}
				}
			}
		}

		public Bundle saveState(Bundle map) {
			synchronized (mSurfaceHolder) {
				if (map != null) {

				}
			}
			return map;
		}

		public void setRunning(boolean b) {
			mRun = b;
		}

		public void setState(int mode) {
			synchronized (mSurfaceHolder) {
				mMode = mode;
			}
		}

		public void setToast() {
			startToast = true;
			beginToast = System.currentTimeMillis();
		}

		public String[] getPlayerActivited() {
			String[] arr = new String[playerActivted.size() - 1];
			int index = 0;
			for (int i = 0; i < playerActivted.size(); i++)
				if (playerActivted.elementAt(i) != getSttMe()) {
					arr[index] = players[playerActivted.elementAt(i)]
							.getAccount();
					index++;
				}
			return arr;
		}

		public void setMeChat(String s) {
			chatMessage = s;
			stt_chat = getSttMe();
			chat = true;
			last_chat = System.currentTimeMillis();
		}

		private int getSttMe() {
			for (int i = 0; i < 4; i++) {
				if (players[i].isMe)
					return i;
			}
			return 0;
		}

		private int getFrontOfPlayer(int stt) {
			int i = stt - 1;
			for (int j = 0; j < 3; j++) {
				if (i >= 0) {
					if (!players[i].getAccount().equals("~"))
						return i;
					else
						i--;
				} else {
					i = 3;
					if (!players[i].getAccount().equals("~"))
						return i;
					else
						i--;
				}
			}

			return stt;
		}

		private int getBehindPlayer(int stt) {
			int i = stt + 1;
			for (int j = 0; j < 3; j++) {
				if (i <= 3) {
					if (!players[i].getAccount().equals("~"))
						return i;
					else
						i++;
				} else {
					i = 0;
					if (!players[i].getAccount().equals("~"))
						return i;
					else
						i++;
				}
			}

			return stt;
		}

		private Vector<Card> getRemovedCardOfPlayer(int stt) {
			if (players[stt].getPosition() == Player.POSITION_6H)
				return removedCard6h;
			else if (players[stt].getPosition() == Player.POSITION_3H)
				return removedCard3h;
			else if (players[stt].getPosition() == Player.POSITION_12H)
				return removedCard12h;
			else
				return removedCard9h;
		}

		private boolean checkPhom(Card card, Vector<Card> card_vector) {
			// Kiem tra xem trong so cac quan bai nhac len co quan nao la quan
			// da an k ?
			for (int i = 0; i < cardAn.size(); i++) {
				for (int j = 0; j < card_vector.size(); j++) {
					if (cardAn.elementAt(i).getId() == card_vector.elementAt(j)
							.getId()) {
						System.out.println("QUAN BAI DA AN!");
						return false;
					}
				}
			}

			// Kiem tra xem co tao phom ngang, phom doc hay k ?
			if (checkPhomNgang(card, card_vector)
					|| checkPhomDoc(card, card_vector)) {
				Vector<Card> temp_vector = new Vector<Card>();
				for (int i = 0; i < card6h.size(); i++)
					temp_vector
							.addElement(new Card(card6h.elementAt(i).getId()));
				for (int i = 0; i < card_vector.size(); i++) {
					for (int j = 0; j < temp_vector.size(); j++) {
						if (temp_vector.elementAt(j).getId() == card_vector
								.elementAt(i).getId()) {
							temp_vector.removeElementAt(j);
						}
					}
				}

				// Kiem tra so luong quan bai duoc phep nhac len
				if (temp_vector.size() < 3 * cardAn.size()) {
					System.out
							.println("SO QUAN BAI CON LAI KHONG DU TAO PHOM!");
					return false;
				}

				for (int i = 0; i < cardAn.size(); i++) {
					int vt = getCardPositionInVector(cardAn.elementAt(i),
							temp_vector);

					if (vt == 0) {
						Vector<Card> temp = new Vector<Card>();
						temp.addElement(temp_vector.elementAt(1));
						temp.addElement(temp_vector.elementAt(2));
						if (checkPhomDoc(cardAn.elementAt(i), temp)
								|| checkPhomNgang(cardAn.elementAt(i), temp)) {
							continue;
						} else {
							System.out
									.println("CAY DA AN O VI TRI 0 KHONG TAO PHOM!");
							return false;
						}
					} else if (vt == temp_vector.size() - 1) {
						Vector<Card> temp = new Vector<Card>();
						temp.addElement(temp_vector.elementAt(temp_vector
								.size() - 2));
						temp.addElement(temp_vector.elementAt(temp_vector
								.size() - 3));
						if (checkPhomDoc(cardAn.elementAt(i), temp)
								|| checkPhomNgang(cardAn.elementAt(i), temp)) {
							continue;
						} else {
							System.out
									.println("CAY DA AN O VI TRI CUOI KHONG TAO PHOM!");
							return false;
						}
					} else {
						Vector<Card> temp = new Vector<Card>();
						temp.addElement(temp_vector.elementAt(vt - 1));
						temp.addElement(temp_vector.elementAt(vt + 1));
						if (checkPhomDoc(cardAn.elementAt(i), temp)
								|| checkPhomNgang(cardAn.elementAt(i), temp)) {
							continue;
						}
						if (vt > 1) {
							temp.removeAllElements();
							temp.addElement(temp_vector.elementAt(vt - 1));
							temp.addElement(temp_vector.elementAt(vt - 2));
							if (checkPhomDoc(cardAn.elementAt(i), temp)
									|| checkPhomNgang(cardAn.elementAt(i), temp))
								continue;
						}
						if (vt < temp_vector.size() - 2) {
							temp.removeAllElements();
							temp.addElement(temp_vector.elementAt(vt + 1));
							temp.addElement(temp_vector.elementAt(vt + 2));
							if (checkPhomDoc(cardAn.elementAt(i), temp)
									|| checkPhomNgang(cardAn.elementAt(i), temp))
								continue;
						}

						System.out.println("CAY DA AN KHONG TAO PHOM!");
						return false;
					}
				}

				System.out.println("PHOM HOP LE, AN THOI!");
				return true;
			} else {
				System.out.println("KHONG TAO PHOM NGANG/DOC!");
				return false;
			}
		}

		private int getCardPositionInVector(Card card, Vector<Card> card_vector) {
			for (int i = 0; i < card_vector.size(); i++) {
				if (card_vector.elementAt(i).getId() == card.getId())
					return i;
			}

			return 0;
		}

		private boolean checkPhomNgang(Card card, Vector<Card> card_vector) {
			if (card_vector.size() > 4) {
				return false;
			} else {
				int temp = card_vector.elementAt(0).so;
				for (int i = 1; i < card_vector.size(); i++) {
					if (temp == card_vector.elementAt(i).so) {
						continue;
					} else {
						return false;
					}
				}

				if (card.so == temp) {
					return true;
				} else {
					return false;
				}
			}
		}

		private boolean checkPhomDoc(Card card, Vector<Card> card_vector) {
			Vector<Card> temp_vector = new Vector<Card>();
			for (int i = 0; i < card_vector.size(); i++) {
				temp_vector.addElement(new Card(card_vector.elementAt(i)
						.getId()));
			}
			int temp = temp_vector.elementAt(0).chat;
			for (int i = 1; i < temp_vector.size(); i++) {
				if (temp == temp_vector.elementAt(i).chat)
					continue;
				else
					return false;
			}

			if (card.chat == temp) {
				temp_vector = sortCard(temp_vector);
				int temp_id = card.getId();
				if ((temp_id + 1) == temp_vector.elementAt(0).getId()
						&& (temp_vector.elementAt(0).getId() + 1) == (temp_vector
								.elementAt(1).getId()))
					return true;
				for (int i = 0; i < temp_vector.size() - 1; i++) {
					if ((temp_vector.elementAt(i).getId() + 1) == temp_id
							&& (temp_id + 1) == (temp_vector.elementAt(i + 1)
									.getId()))
						return true;
				}
				int number = temp_vector.size();
				if ((temp_vector.elementAt(number - 2).getId() + 1) == (temp_vector
						.elementAt(number - 1).getId())
						&& (temp_vector.elementAt(number - 1).getId() + 1) == temp_id)
					return true;
				return false;
			} else {
				return false;
			}
		}

		private boolean checkPhomWhenMove(Vector<Card> card_vector) {

			for (int i = 0; i < cardAn.size(); i++) {
				int vt = getCardPositionInVector(cardAn.elementAt(i),
						card_vector);

				if (vt == 0) {
					Vector<Card> temp = new Vector<Card>();
					temp.addElement(card_vector.elementAt(1));
					temp.addElement(card_vector.elementAt(2));
					if (checkPhomDoc(cardAn.elementAt(i), temp)
							|| checkPhomNgang(cardAn.elementAt(i), temp)) {
						continue;
					} else {
						System.out
								.println("MOVING: Quan bai an o vi tri 0 khong tao phom");
						return false;
					}
				} else if (vt == card_vector.size() - 1) {
					Vector<Card> temp = new Vector<Card>();
					temp.addElement(card_vector.elementAt(card_vector.size() - 2));
					temp.addElement(card_vector.elementAt(card_vector.size() - 3));
					if (checkPhomDoc(cardAn.elementAt(i), temp)
							|| checkPhomNgang(cardAn.elementAt(i), temp)) {
						continue;
					} else {
						System.out
								.println("MOVING: Quan bai an o vi tri cuoi khong tao phom");
						return false;
					}
				} else {
					Vector<Card> temp = new Vector<Card>();
					temp.addElement(card_vector.elementAt(vt - 1));
					temp.addElement(card_vector.elementAt(vt + 1));
					if (checkPhomDoc(cardAn.elementAt(i), temp)
							|| checkPhomNgang(cardAn.elementAt(i), temp)) {
						continue;
					}
					if (vt > 1) {
						temp.removeAllElements();
						temp.addElement(card_vector.elementAt(vt - 1));
						temp.addElement(card_vector.elementAt(vt - 2));
						if (checkPhomDoc(cardAn.elementAt(i), temp)
								|| checkPhomNgang(cardAn.elementAt(i), temp)) {
							continue;
						}
					}
					if (vt < card_vector.size() - 2) {
						temp.removeAllElements();
						temp.addElement(card_vector.elementAt(vt + 1));
						temp.addElement(card_vector.elementAt(vt + 2));
						if (checkPhomDoc(cardAn.elementAt(i), temp)
								|| checkPhomNgang(cardAn.elementAt(i), temp)) {
							continue;
						}
					}

					System.out.println("MOVING: Quan bai an o vi tri " + vt
							+ "khong tao phom");
					return false;
				}
			}
			return true;
		}

		private boolean checkCardDanh(Card card, Vector<Card> card_vector) {
			for (int i = 0; i < cardAn.size(); i++) {
				if (card.getId() == cardAn.elementAt(i).getId()) {
					System.out.println("DANH QUAN BAI DA AN!");
					return false;
				}
			}

			Vector<Card> temp_card = new Vector<Card>();
			for (int i = 0; i < card_vector.size(); i++) {
				temp_card
						.addElement(new Card(card_vector.elementAt(i).getId()));
			}

			for (int i = 0; i < temp_card.size(); i++) {
				if (temp_card.elementAt(i).getId() == card.getId()) {
					temp_card.removeElementAt(i);
					break;
				}
			}

			return checkPhomWhenMove(temp_card);
		}

		private Vector<Card> sortCard(Vector<Card> card_vector) {
			Card[] temp_arr = new Card[card_vector.size()];
			for (int i = 0; i < card_vector.size(); i++)
				temp_arr[i] = card_vector.elementAt(i);
			for (int i = 0; i < temp_arr.length; i++)
				for (int j = temp_arr.length - 1; j > i; j--) {
					if (temp_arr[j].so < temp_arr[j - 1].so) {
						Card temp;
						temp = temp_arr[j];
						temp_arr[j] = temp_arr[j - 1];
						temp_arr[j - 1] = temp;
					}
				}
			card_vector.removeAllElements();
			for (int i = 0; i < temp_arr.length; i++)
				card_vector.addElement(temp_arr[i]);
			return card_vector;
		}

		private Vector<Integer> sortIntOfCard(Vector<Integer> vector) {
			int[] temp_arr = new int[vector.size()];
			for (int i = 0; i < vector.size(); i++)
				temp_arr[i] = vector.elementAt(i);
			vector.removeAllElements();
			for (int i = 0; i < temp_arr.length; i++)
				for (int j = temp_arr.length - 1; j > i; j--) {
					if ((temp_arr[j] % 13) < (temp_arr[j - 1] % 13)) {
						int temp;
						temp = temp_arr[j];
						temp_arr[j] = temp_arr[j - 1];
						temp_arr[j - 1] = temp;
					}
				}
			for (int i = 0; i < temp_arr.length; i++)
				vector.addElement(temp_arr[i]);
			return vector;
		}

		private Player[] sortKetQua() {
			Player[] temp = new Player[playerActivted.size()];
			for (int i = 0; i < playerActivted.size(); i++) {
				temp[i] = players[playerActivted.elementAt(i)];
			}

			for (int i = 0; i < temp.length; i++)
				for (int j = temp.length - 1; j > i; j--) {
					if (temp[j].getKetQua() < temp[j - 1].getKetQua()) {
						Player p;
						p = temp[j];
						temp[j] = temp[j - 1];
						temp[j - 1] = p;
					}
				}
			return temp;
		}

		private boolean checkPhomNgang(Vector<Card> card_vector) {
			if (card_vector.size() > 4) {
				return false;
			} else {
				int temp = card_vector.elementAt(0).so;
				for (int i = 1; i < card_vector.size(); i++) {
					if (temp == card_vector.elementAt(i).so) {
						continue;
					} else {
						return false;
					}
				}
				return true;
			}
		}

		private boolean checkPhomDoc(Vector<Card> card_vector) {
			Vector<Card> temp_vector = new Vector<Card>();
			for (int i = 0; i < card_vector.size(); i++) {
				temp_vector.addElement(new Card(card_vector.elementAt(i)
						.getId()));
			}
			temp_vector = sortCard(temp_vector);
			int temp = temp_vector.elementAt(0).chat;
			for (int i = 1; i < temp_vector.size(); i++) {
				if (temp == temp_vector.elementAt(i).chat)
					continue;
				else
					return false;
			}

			int temp_id = temp_vector.elementAt(0).getId();
			for (int i = 1; i < temp_vector.size(); i++) {
				if (temp_vector.elementAt(i).getId() == temp_id + 1) {
					temp_id = temp_vector.elementAt(i).getId();
					continue;
				} else {
					return false;
				}
			}
			return true;
		}

		private Vector<Card> updatePhom(Vector<Card> vector_card) {
			for (int i = 0; i < vector_card.size(); i++) {
				vector_card.elementAt(i).sign = false;
			}
			int i = 0;
			int count = 3;
			if (vector_card.size() >= 3)
				while (true) {
					Vector<Card> temp_vector = new Vector<Card>();
					for (int j = i; j < count; j++) {
						temp_vector.addElement(vector_card.elementAt(j));
					}
					// temp_vector = sortCard(temp_vector);
					if (checkPhomDoc(temp_vector)
							|| checkPhomNgang(temp_vector)) {
						for (int j = i; j < count; j++) {
							vector_card.elementAt(j).sign = true;
						}
						count++;
						if (count > vector_card.size())
							break;
					} else {
						if (vector_card.elementAt(count - 2).sign) {
							i = count - 1;
						} else {
							i++;
						}
						count = i + 3;
						if (count > vector_card.size())
							break;
					}
				}

			return vector_card;
		}

		private int checkChot(int stt) {
			if (getRemovedCardOfPlayer(stt).size() == 4) {
				System.out.println("---------CHOT-----------");
				return CHOT;
			} else if (getRemovedCardOfPlayer(stt).size() == 3) {
				if (getRemovedCardOfPlayer(getBehindPlayer(stt)).size() == 3) {
					System.out.println("---------CHOT--------");
					return CHOT;
				} else {
					return THUONG;
				}
			}
			return THUONG;
		}

		private void nextLuotDanhBai() {
			luotDanh++;
			stt_db = 0;
			if (luotDanh > playerActivted.size() - 1)
				luotDanh = 0;
			if (playerActivted.elementAt(luotDanh) == getSttMe()) {
				updateButton();
			}

			if (getRemovedCardOfPlayer(playerActivted.elementAt(luotDanh))
					.size() == 4) {
				((PhomActivity) mContext).sendFNData();
			}
		}

		private void setPositionCard6h() {
			XBegin6h = (mCanvasWidth - (card6h.size() - 1) * d6h - cardWidth)
					/ 2 + arX;
			for (int i = 0; i < card6h.size(); i++) {
				card6h.elementAt(i).setX(XBegin6h + d6h * i);
				card6h.elementAt(i).setY(arY - cardHeight);
			}
		}

		private void setPositionRemovedCard6h(int n) {
			XBegin6h_removed = (mCanvasWidth - (n - 1) * d6h_removed - cardWidth) / 2;
			for (int i = 0; i < removedCard6h.size(); i++) {
				removedCard6h.elementAt(i).setX(
						XBegin6h_removed + d6h_removed * i);
				removedCard6h.elementAt(i).setY(arY - 3 * cardHeight / 2);
			}
		}

		private void setPositionPhom6h() {
			XBegin6h_phom = (mCanvasWidth - (card6h_phom.size() - 1) * d6h_phom - cardWidth) / 2;
			for (int i = 0; i < card6h_phom.size(); i++) {
				card6h_phom.elementAt(i).setX(XBegin6h_phom + d6h_phom * i);
				card6h_phom.elementAt(i).setY(arY - 2 * cardHeight);
			}
		}

		private void setPositionCard3h(int n, int d) {
			YBegin3h = (mCanvasHeight - (n - 1) * d - cardHeight) / 2 - deltaY;
			for (int i = 0; i < card3h.size(); i++) {
				card3h.elementAt(i).setX(420 * mCanvasWidth / 480);
				card3h.elementAt(i).setY(YBegin3h + d * i);
			}
		}

		private void setPositionRemovedCard3h(int n) {
			YBegin3h_removed = (mCanvasHeight - (n - 1) * d3h_removed - cardHeight)
					/ 2 - deltaY;
			for (int j = 0; j < removedCard3h.size(); j++) {
				removedCard3h.elementAt(j).setX(385 * mCanvasWidth / 480);
				removedCard3h.elementAt(j).setY(
						YBegin3h_removed + d3h_removed * j);
			}
		}

		private void setPositionPhom3h() {
			YBegin3h_phom = (mCanvasHeight - (card3h_phom.size() - 1)
					* d3h_phom - cardHeight)
					/ 2 - deltaY;
			for (int i = 0; i < card3h_phom.size(); i++) {
				card3h_phom.elementAt(i).setX(335 * mCanvasWidth / 480);
				card3h_phom.elementAt(i).setY(YBegin3h_phom + d3h_phom * i);
			}
		}

		private void setPositionCard12h(int n, int d) {
			XBegin12h = (mCanvasWidth - (n - 1) * d - cardWidth) / 2;
			for (int i = 0; i < card12h.size(); i++) {
				card12h.elementAt(i).setX(XBegin12h + d * i);
				card12h.elementAt(i).setY(5 * mCanvasHeight / 320);
			}
		}

		private void setPositionRemovedCard12h(int n) {
			XBegin12h_removed = (mCanvasWidth - (n - 1) * d12h_removed - cardWidth) / 2;
			for (int i = 0; i < removedCard12h.size(); i++) {
				removedCard12h.elementAt(i).setX(
						XBegin12h_removed + d12h_removed * i);
				removedCard12h.elementAt(i).setY(30 * mCanvasHeight / 320);
			}
		}

		private void setPositionPhom12h() {
			XBegin12h_phom = (mCanvasWidth - (card12h_phom.size() - 1)
					* d12h_phom - cardWidth) / 2;
			for (int i = 0; i < card12h_phom.size(); i++) {
				card12h_phom.elementAt(i).setX(XBegin12h_phom + d12h_phom * i);
				card12h_phom.elementAt(i).setY(55 * mCanvasHeight / 320);
			}
		}

		private void setPositionCard9h(int n, int d) {
			YBegin9h = (mCanvasHeight - (n - 1) * d - cardHeight) / 2 - deltaY;
			for (int i = 0; i < card9h.size(); i++) {
				card9h.elementAt(i).setX(10 * mCanvasWidth / 480);
				card9h.elementAt(i).setY(YBegin9h + d * i);
			}
		}

		private void setPositionRemovedCard9h(int n) {
			YBegin9h_removed = (mCanvasHeight - (n - 1) * d9h_removed - cardHeight)
					/ 2 - deltaY;
			for (int i = 0; i < removedCard9h.size(); i++) {
				removedCard9h.elementAt(i).setX(45 * mCanvasWidth / 480);
				removedCard9h.elementAt(i).setY(
						YBegin9h_removed + d9h_removed * i);
			}
		}

		private void setPositionPhom9h() {
			YBegin9h_phom = (mCanvasHeight - (card9h_phom.size() - 1)
					* d9h_phom - cardHeight)
					/ 2 - deltaY;
			for (int i = 0; i < card9h_phom.size(); i++) {
				card9h_phom.elementAt(i).setX(95 * mCanvasWidth / 480);
				card9h_phom.elementAt(i).setY(YBegin9h_phom + d9h_phom * i);
			}
		}

		private void setDanhBai() {
			((PhomActivity) mContext).sendDBData(getSttMe(),
					card6h.elementAt(stt_db).getId());

			tempCard.removeAllElements();
			Card card = new Card(card6h.elementAt(stt_db).getX(), card6h
					.elementAt(stt_db).getY(), MS_PER_FRAME, 53, card6h
					.elementAt(stt_db).getId());
			XBegin6h_removed = (mCanvasWidth - removedCard6h.size()
					* d6h_removed - cardWidth) / 2;
			card.setDist(XBegin6h_removed + removedCard6h.size() * d6h_removed,
					arY - 3 * cardHeight / 2);
			card.state = Card.MOVING;
			tempCard.addElement(card);

			source = tempCard;
			target = removedCard6h;

			card6h.removeElementAt(stt_db);
			so_bai_truoc_mat = removedCard6h.size() + 1;
			setPositionCard6h();

			setPositionRemovedCard6h(removedCard6h.size() + 1);
			moving = true;
		}

		private void setAnBai(Card card) {
			((PhomActivity) mContext).sendEARequest(
					getFrontOfPlayer(getSttMe()), card.getId(),
					checkChot(db_data.elementAt(0)));
		}

		private boolean checkU(Vector<Card> card_vector) {
			int count = 0;
			for (int i = 0; i < card_vector.size(); i++) {
				if (card_vector.elementAt(i).sign)
					count++;
			}

			if (count >= card_vector.size() - 1)
				return true;
			else
				return false;
		}

		private void updateButton() {
			if (removedCard6h.size() == 0 && card6h.size() == 10) {
				display_danh = true;
				update_button = UPDATE_BUTTON0;
			} else if (removedCard6h.size() == 0 && card6h.size() == 9) {
				display_boc = true;
				display_an = true;
				update_button = UPDATE_BUTTON0;
			} else if (removedCard6h.size() == 1) {
				display_boc = true;
				display_an = true;
				update_button = UPDATE_BUTTON1;
			} else if (removedCard6h.size() == 2) {
				display_boc = true;
				display_an = true;
				update_button = UPDATE_BUTTON2;
			} else if (removedCard6h.size() == 3) {
				display_boc = true;
				display_an = true;
				update_button = UPDATE_BUTTON3;
			}
		}

		private void setHaPhom() {
			for (int i = 0; i < card6h.size(); i++) {
				if (card6h.elementAt(i).sign)
					card6h_phom.addElement(new Card(0, 0, MS_PER_FRAME, 53,
							card6h.elementAt(i).getId()));
			}

			for (int i = 0; i < card6h.size(); i++) {
				for (int j = 0; j < card6h_phom.size(); j++) {
					if (card6h.elementAt(i).getId() == card6h_phom.elementAt(j)
							.getId())
						card6h.removeElementAt(i);
				}
			}

			cardAn.removeAllElements();

			setPositionPhom6h();

			Vector<Integer> v = new Vector<Integer>();
			for (int i = 0; i < card6h_phom.size(); i++) {
				v.addElement(card6h_phom.elementAt(i).getId());
			}

			((PhomActivity) mContext).sendHPData(getSttMe(), v);

			if (!checkU) {
				if (denLang)
					((PhomActivity) mContext).sendUData("+"
							+ getFrontOfPlayer(getSttMe()));
				else
					((PhomActivity) mContext).sendUData("-1");
			}

			card6h = sortCard(card6h);
			card6h = updatePhom(card6h);
			setPositionCard6h();

			for (int i = 0; i < card6h.size(); i++) {
				if (card6h.elementAt(i).sign) {
					display_ha_phom = true;
					display_danh = false;
					break;
				}
			}
		}

		public void unpause() {
			setState(STATE_RUNNING);
		}

		boolean doKeyDown(int keyCode, KeyEvent msg) {
			synchronized (mSurfaceHolder) {
				return false;
			}
		}

		boolean doKeyUp(int keyCode, KeyEvent msg) {
			synchronized (mSurfaceHolder) {
				if (mMode == STATE_RUNNING) {

				}
			}

			return false;
		}

		boolean doTouch(MotionEvent event) {
			int tx = (int) event.getX();
			int ty = (int) event.getY();
			int action = event.getAction();

			synchronized (mSurfaceHolder) {
				if (chat_button.isTouch(tx, ty)) {
					// input.toggleSoftInput(0, 0);
					((PhomActivity) mContext).showDialogChat();
					// ((PhomActivity)mContext).showDialogMoneySetting();
					// ((PhomActivity)mContext).showDialogUserSetting();
					// ((PhomActivity) mContext).showDialogKick();
				}
				if (isPlay) {
					if (isContinue) {
						if (continue_button.isTouch(tx, ty)) {
							isPlay = false;
							clear = true;
							initAgain();
							((PhomActivity) mContext).sendTCData();
						}
					}

					if (boc_button.isTouch(tx, ty) && display_boc) {
						display_boc = false;
						display_an = false;
						if (update_button == UPDATE_BUTTON0
								|| update_button == UPDATE_BUTTON1
								|| update_button == UPDATE_BUTTON2)
							display_danh = true;
						else if (update_button == UPDATE_BUTTON3)
							display_ha_phom = true;
						((PhomActivity) mContext).sendNBRequest();
						return false;
					}

					if (danh_button.isTouch(tx, ty) && display_danh) {
						display_danh = false;
						int count = 0;
						for (int i = 0; i < card6h.size(); i++) {
							if (card6h.elementAt(i).getIsChoise(
									arY - cardHeight))
								count++;
						}

						if (count > 1) {
							((PhomActivity) mContext)
									.setToast("Chọn một quân bài để đánh");
							display_danh = true;
							return false;
						}

						if (checkCardDanh(card6h.elementAt(stt_db), card6h))
							setDanhBai();
						else {
							((PhomActivity) mContext)
									.setToast("Đánh bài không hợp lệ!");
							display_danh = true;
						}

						return false;
					}

					if (an_button.isTouch(tx, ty) && display_an) {
						display_an = false;
						if (update_button == UPDATE_BUTTON3) {
							display_danh = false;
							display_ha_phom = true;
						} else {
							display_danh = true;
						}
						display_boc = false;
						choiseCard.removeAllElements();
						for (int i = 0; i < card6h.size(); i++) {
							if (card6h.elementAt(i).getIsChoise(
									arY - cardHeight))
								choiseCard.addElement(card6h.elementAt(i));
						}
						if (choiseCard.size() < 2) {
							((PhomActivity) mContext)
									.setToast("Chọn ít nhất 2 quân bài để ăn");
							display_an = true;
							display_danh = false;
							display_ha_phom = false;
							display_boc = true;
							return false;
						}

						Card card = new Card(0, 0, MS_PER_FRAME, 53,
								db_data.elementAt(1));
						boolean isPhom = false;
						isPhom = checkPhom(card, choiseCard);
						if (isPhom) {
							setAnBai(card);
						} else {
							((PhomActivity) mContext)
									.setToast("Phỏm không hợp lệ");
							display_an = true;
							display_danh = false;
							display_ha_phom = false;
							display_boc = true;
						}
						return false;
					}

					if (ha_button.isTouch(tx, ty) && display_ha_phom) {
						display_danh = true;
						display_ha_phom = false;
						start_ha_phom = true;
						// setHaPhom();

						return false;
					}

					if (touchFlag)
						for (int i = card6h.size() - 1; i >= 0; i--) {
							if (card6h.elementAt(i).isTouched(tx, ty)) {
								isTouch = true;
								touchFlag = false;
								seqCard = i;
								card6hSaved.removeAllElements();
								for (int j = 0; j < card6h.size(); j++)
									card6hSaved.addElement(new Card(card6h
											.elementAt(j).getX(), card6h
											.elementAt(j).getY(), MS_PER_FRAME,
											53, card6h.elementAt(j).getId()));
								break;
							}
						}
					if (isTouch) {
						switch (action) {
						case MotionEvent.ACTION_DOWN:
							initX = (int) event.getX();
							initY = (int) event.getY();
							if (card6h.elementAt(seqCard).getIsChoise(
									arY - cardHeight)) {
								card6h.elementAt(seqCard)
										.setY(arY - cardHeight);
							} else {
								stt_db = seqCard;
								card6h.elementAt(seqCard).setY(
										arY - 4 * cardHeight / 3);
							}
							movedCard = card6h.elementAt(seqCard);
							return true;
						case MotionEvent.ACTION_MOVE:
							if (((Math.abs(event.getX() - initX)) + (Math
									.abs(event.getY() - initY))) > 10) {
								isDrap = true;
								card6h.elementAt(seqCard).setX(
										tx - cardWidth / 2);
								card6h.elementAt(seqCard).setY(ty - cardHeight);
							}
							return true;
						case MotionEvent.ACTION_UP:
							touchFlag = true;
							isTouch = false;
							if (isDrap) {
								isDrap = false;
								Vector<Card> temp = new Vector<Card>();
								for (int i = 0; i < card6h.size(); i++) {
									temp.addElement(new Card(card6h
											.elementAt(i).getId()));
								}
								if (checkPhomWhenMove(temp)) {
									setPositionCard6h();
								} else {
									card6h.removeAllElements();
									for (int i = 0; i < card6hSaved.size(); i++)
										card6h.addElement(card6hSaved
												.elementAt(i));
								}
							}
							return true;
						}
						return true;
					}

				} else {
					if (readyButton.isTouch(tx, ty)) {
						boolean sendRequest = false;
						int count = 0;
						if (getSttMe() == boss) {
							for (int i = 0; i < playerActivted.size(); i++) {
								if (playerActivted.elementAt(i) != getSttMe()
										&& players[playerActivted.elementAt(i)]
												.getState() == Player.READY) {
									count++;
								}
							}

							if (count == playerActivted.size() - 1) {
								sendRequest = true;
							} else {
								((PhomActivity) mContext)
										.setToast("Người chơi chưa sẵn sàng");
								sendRequest = false;
							}
						} else {
							sendRequest = true;
						}
						if (sendRequest) {
							if (getSttMe() == boss) {
								((PhomActivity) mContext).sendReadyData();
							} else {
								if (players[getSttMe()].getState() != Player.READY)
									((PhomActivity) mContext).sendReadyData();
								else
									((PhomActivity) mContext)
											.sendNotReadyData();
							}
						}
					}
				}
			}

			return false;
		}

		private void doDraw(Canvas canvas) {
			// canvas.drawBitmap(background, 0, 0, null);
			canvas.drawBitmap(background, src, dst, null);
			canvas.drawBitmap(logo, 3, 3, null);
			if (isPlay) {
				// cardBoc.draw(canvas);

				if (drawClock) {
					canvas.drawText("" + clock(), mCanvasWidth / 2 - 9,
							mCanvasHeight / 2 - 7, paint);
				}

				if (display_an)
					an_button.draw(canvas);
				if (display_danh)
					danh_button.draw(canvas);
				if (display_ha_phom)
					ha_button.draw(canvas);
				if (display_boc)
					boc_button.draw(canvas);
				nap_ycoin_button.draw(canvas);

				for (int i = 0; i < 4; i++) {
					if (!players[i].isHiden)
						players[i].draw(canvas);
				}

				if (!isContinue)
					for (int i = 0; i < card6h_phom.size(); i++)
						card6h_phom.elementAt(i).draw(canvas);
				if (!isContinue)
					for (int i = 0; i < removedCard6h.size(); i++)
						removedCard6h.elementAt(i).draw(canvas);

				if (isDrap) {
					for (int i = 0; i < card6h.size(); i++)
						if (i != seqCard)
							canvas.drawBitmap(card6h.elementAt(i).frame,
									XBegin6h + d6h * i, arY - cardHeight, null);
					movedCard.draw(canvas);
				} else {
					for (int i = 0; i < card6h.size(); i++)
						card6h.elementAt(i).draw(canvas);
				}

				for (int i = 0; i < card3h.size(); i++)
					card3h.elementAt(i).draw(canvas);
				if (!isContinue) {
					for (int i = 0; i < removedCard3h.size(); i++)
						removedCard3h.elementAt(i).draw(canvas);
					for (int i = 0; i < card3h_phom.size(); i++)
						card3h_phom.elementAt(i).draw(canvas);
				}

				for (int i = 0; i < card12h.size(); i++)
					card12h.elementAt(i).draw(canvas);
				if (!isContinue) {
					for (int i = 0; i < removedCard12h.size(); i++)
						removedCard12h.elementAt(i).draw(canvas);
					for (int i = 0; i < card12h_phom.size(); i++)
						card12h_phom.elementAt(i).draw(canvas);
				}

				for (int i = 0; i < card9h.size(); i++)
					card9h.elementAt(i).draw(canvas);
				if (!isContinue) {
					for (int i = 0; i < removedCard9h.size(); i++)
						removedCard9h.elementAt(i).draw(canvas);
					for (int i = 0; i < card9h_phom.size(); i++)
						card9h_phom.elementAt(i).draw(canvas);
				}

				for (int i = 0; i < tempCard.size(); i++)
					tempCard.elementAt(i).draw(canvas);

				if (draw_money_effect) {
					drawMoneyEffect(canvas);
				}

				if (drawClock)
					drawTimeBar(canvas);

			} else {
				// canvas.drawBitmap(table, mCanvasWidth / 2 - table.getWidth()
				// / 2, (mCanvasHeight + mTitleBar.getHeight()) / 2
				// - table.getHeight() / 2, null);
				for (int i = 0; i < players.length; i++) {
					if (!players[i].isHiden)
						players[i].draw(canvas);
				}
				readyButton.draw(canvas);
				nap_ycoin_button.draw(canvas);
			}

			if (isContinue) {
				continue_button.draw(canvas);
				YBeginFrame = (mCanvasHeight - (ketqua.length + 1)
						* frame.getHeight()) / 2;
				canvas.drawBitmap(frame, (mCanvasWidth - frame.getWidth()) / 2,
						YBeginFrame, null);
				for (int i = 0; i < ketqua.length; i++) {
					canvas.drawBitmap(frame1,
							(mCanvasWidth - frame.getWidth()) / 2, YBeginFrame
									+ frame.getHeight() * (i + 1), null);
					canvas.drawText("" + (i + 1),
							(mCanvasWidth - frame.getWidth()) / 2 + 15,
							YBeginFrame + frame.getHeight() * (i + 1) + 20,
							textPaint1);
					canvas.drawText(ketqua[i].getAccDisplay(),
							(mCanvasWidth - frame.getWidth()) / 2 + 80,
							YBeginFrame + frame.getHeight() * (i + 1) + 20,
							textPaint1);
					String money = Integer.toString(ketqua[i]
							.getTotalMoneyEffect());
					if (ketqua[i].getTotalMoneyEffect() > 0) {
						money = "+" + money;
					}
					canvas.drawText(money,
							(mCanvasWidth - frame.getWidth()) / 2 + 180,
							YBeginFrame + frame.getHeight() * (i + 1) + 20,
							textPaint1);
					if (!hasU && ketqua[i].getKetQua() == Player.MOM)
						canvas.drawText("Móm",
								(mCanvasWidth - frame.getWidth()) / 2 + 265,
								YBeginFrame + frame.getHeight() * (i + 1) + 20,
								textPaint2);
					if (ketqua[i].getKetQua() == Player.U) {
						hasU = true;
						canvas.drawText("Ù",
								(mCanvasWidth - frame.getWidth()) / 2 + 290,
								YBeginFrame + frame.getHeight() * (i + 1) + 20,
								textPaint2);
					}

					if (ketqua[i].getKetQua() == Player.U_DEN) {
						hasU = true;
						canvas.drawText("Ù đền",
								(mCanvasWidth - frame.getWidth()) / 2 + 265,
								YBeginFrame + frame.getHeight() * (i + 1) + 20,
								textPaint2);
					}

					if (ketqua[i].getKetQua() == Player.DEN_LANG) {
						canvas.drawText("Đền làng",
								(mCanvasWidth - frame.getWidth()) / 2 + 265,
								YBeginFrame + frame.getHeight() * (i + 1) + 20,
								textPaint2);
					}
					if (!hasU && ketqua[i].getKetQua() == Player.THANG) {
						canvas.drawText("Thắng",
								(mCanvasWidth - frame.getWidth()) / 2 + 265,
								YBeginFrame + frame.getHeight() * (i + 1) + 20,
								textPaint2);
					}
				}
			}

			chat_button.draw(canvas);

			// if (chat) {
			// long time = System.currentTimeMillis();
			// if (time < last_chat + time_for_chat) {
			// drawChatMessage(stt_chat, chatMessage, canvas);
			// } else {
			// chat = false;
			// }
			// }
		}

		private void updatePhysics() {
			if (isPlay) {
				if (!isDrap && !moving && !chia_bai && !start_ha_phom)
					card6h = updatePhom(card6h);

				if (start_ha_phom) {
					setHaPhom();
					start_ha_phom = false;
				}

				if (card6h.size() == 10 && checkU(card6h) && checkU) {
					display_danh = false;
					display_ha_phom = true;
					display_an = false;
					checkU = false;
					for (int i = 0; i < players.length; i++) {
						if (players[i].isMe && players[i].checkUDen()) {
							denLang = true;
							System.out.println("----------U DEN---------");
							break;
						}
					}
				}

				if (isDrap) {
					for (int i = 0; i < card6h.size(); i++) {
						if (card6h.elementAt(i).isInsert(
								card6h.elementAt(seqCard).getX() + cardWidth
										/ 2,
								card6h.elementAt(seqCard).getY() + cardHeight)) {
							card6h.removeElementAt(seqCard);
							card6h.insertElementAt(movedCard, i);
							seqCard = i;
						}
					}
					if (((card6h.elementAt(seqCard).getX() + cardWidth / 2) < XBegin6h)
							&& ((card6h.elementAt(seqCard).getY() + cardHeight) > (arY - 4 * cardHeight / 3))) {
						card6h.removeElementAt(seqCard);
						card6h.insertElementAt(movedCard, 0);
						seqCard = 0;
					}
				}

				if (chia_bai) {
					if (tempCard.size() > 0) {
						moving = true;
						if (chia) {
							XBegin6h = (mCanvasWidth - (numberCard - 1) * d6h - cardWidth)
									/ 2 + arX;
							tempCard.elementAt(0)
									.setDist(XBegin6h + dem_chia * d6h,
											arY - cardHeight);
							tempCard.elementAt(0).state = Card.MOVING;
							chia = false;
						}
					} else {
						moving = false;
						chia_bai = false;
						if (card6h.size() == 10) {
							updateButton();
							setPositionCard6h();
						}
					}
				}

				if (moving) {
					for (int i = 0; i < source.size(); i++) {
						if (source.elementAt(i).state == Card.MOVING) {
							long time = System.currentTimeMillis();
							source.elementAt(i).move(time);
						} else if (source.elementAt(i).state == Card.REMOVING) {
							Card card = new Card(source.elementAt(i).getX(),
									source.elementAt(i).getY(), MS_PER_FRAME,
									53, source.elementAt(i).getId());
							if (!an_bai_6h)
								target.addElement(card);
							else
								an_bai_6h = false;
							source.removeElementAt(i);
							if (source.size() == 0)
								moving = false;
							if (chia_bai) {
								chia = true;
								dem_chia++;
							}
						}
					}
				}

				if (startToast) {
					long time = System.currentTimeMillis();
					if (time > beginToast + timeForToast) {
						((PhomActivity) mContext).stopToast();
						startToast = false;
					}
				}
			} else {
				if (clear) {
					System.gc();
					clear = false;
				}
			}
		}

		private void setTime() {
			time = 30;
		}

		private void drawTimeBar(Canvas canvas) {
			if (players[playerActivted.elementAt(luotDanh)].getPosition() == Player.POSITION_12H) {
				horizonSrcRect.right = time * 2;
				horizonDstRect.left = mCanvasWidth / 2
						- time_bar_full_horizontal.getWidth() / 2;
				// horizonDstRect.top = mCanvasHeight / 2
				// - time_bar_full_horizontal.getHeight() / 2 - 100 * scale;
				horizonDstRect.top = players[playerActivted.elementAt(luotDanh)]
						.getY()
						+ players[playerActivted.elementAt(luotDanh)]
								.getHeight();
				horizonDstRect.right = horizonDstRect.left + time * 2;
				horizonDstRect.bottom = horizonDstRect.top
						+ time_bar_full_horizontal.getHeight();
				canvas.drawBitmap(
						time_bar_zero_horizontal,
						mCanvasWidth / 2 - time_bar_full_horizontal.getWidth()
								/ 2,
						players[playerActivted.elementAt(luotDanh)].getY()
								+ players[playerActivted.elementAt(luotDanh)]
										.getHeight(), null);
				canvas.drawBitmap(time_bar_full_horizontal, horizonSrcRect,
						horizonDstRect, null);
			} else if (players[playerActivted.elementAt(luotDanh)]
					.getPosition() == Player.POSITION_9H
					|| players[playerActivted.elementAt(luotDanh)]
							.getPosition() == Player.POSITION_3H) {
				horizonSrcRect.right = time * 2;
				horizonDstRect.left = players[playerActivted
						.elementAt(luotDanh)].getX()
						+ players[playerActivted.elementAt(luotDanh)]
								.getWidth() / 2;
				horizonDstRect.top = players[playerActivted.elementAt(luotDanh)]
						.getY() - 2 * time_bar_full_horizontal.getHeight();
				horizonDstRect.right = horizonDstRect.left + time * 2;
				horizonDstRect.bottom = horizonDstRect.top
						+ time_bar_full_horizontal.getHeight();
				canvas.drawBitmap(
						time_bar_zero_horizontal,
						players[playerActivted.elementAt(luotDanh)].getX()
								+ players[playerActivted.elementAt(luotDanh)]
										.getWidth() / 2,
						players[playerActivted.elementAt(luotDanh)].getY() - 2
								* time_bar_full_horizontal.getHeight(), null);
				canvas.drawBitmap(time_bar_full_horizontal, horizonSrcRect,
						horizonDstRect, null);
			} else if (players[playerActivted.elementAt(luotDanh)]
					.getPosition() == Player.POSITION_6H) {
				horizonSrcRect.right = time * 2;
				horizonDstRect.left = mCanvasWidth / 2
						- time_bar_full_horizontal.getWidth() / 2;
				horizonDstRect.top = players[playerActivted.elementAt(luotDanh)]
						.getY() - 3 * time_bar_full_horizontal.getHeight();
				horizonDstRect.right = horizonDstRect.left + time * 2;
				horizonDstRect.bottom = horizonDstRect.top
						+ time_bar_full_horizontal.getHeight();
				canvas.drawBitmap(time_bar_zero_horizontal, mCanvasWidth / 2
						- time_bar_full_horizontal.getWidth() / 2,
						players[playerActivted.elementAt(luotDanh)].getY() - 3
								* time_bar_full_horizontal.getHeight(), null);
				canvas.drawBitmap(time_bar_full_horizontal, horizonSrcRect,
						horizonDstRect, null);
			}
		}

		// private void drawChatMessage(int stt, String s, Canvas canvas) {
		// if (players[stt].getPosition() == Player.POSITION_12H) {
		// byte[] b = new byte[s.length()];
		// b = PiPoDesigner.toByteIndex(s);
		// int length = PiPoDesigner.getLengString(b,
		// PiPoDesigner.COLOR_WHITE, PiPoDesigner.COLOR_PINK, 0,
		// b.length);
		// for (int i = 0; i < length / 2; i++) {
		// canvas.drawBitmap(chat_middle, mCanvasWidth / 2 + i, 5,
		// null);
		// }
		// for (int i = 0; i < length / 2 + 5; i++) {
		// canvas.drawBitmap(chat_middle, mCanvasWidth / 2 - (i + 1),
		// 5, null);
		// }
		// canvas.drawBitmap(chat_header_right, mCanvasWidth / 2 - length
		// / 2 - chat_header_right.getWidth(), 5, null);
		// canvas.drawBitmap(chat_footer_right, mCanvasWidth / 2 + length
		// / 2, 5, null);
		// PiPoDesigner.drawCenterString(canvas, mCanvasWidth / 2, 12, b,
		// PiPoDesigner.COLOR_WHITE, PiPoDesigner.COLOR_PINK, 0,
		// b.length);
		// } else if (players[stt].getPosition() == Player.POSITION_9H) {
		// byte[] b = new byte[s.length()];
		// b = PiPoDesigner.toByteIndex(s);
		// int length = PiPoDesigner.getLengString(b,
		// PiPoDesigner.COLOR_WHITE, PiPoDesigner.COLOR_PINK, 0,
		// b.length);
		// for (int i = 0; i < length / 2; i++) {
		// canvas.drawBitmap(chat_middle, 80 + i, mCanvasHeight / 2
		// - chat_middle.getHeight() / 2, null);
		// }
		// for (int i = 0; i < length / 2 + 5; i++) {
		// canvas.drawBitmap(chat_middle, 80 - (i + 1), mCanvasHeight
		// / 2 - chat_middle.getHeight() / 2, null);
		// }
		// canvas.drawBitmap(chat_header_right, 80 - length / 2
		// - chat_header_right.getWidth(), mCanvasHeight / 2
		// - chat_middle.getHeight() / 2, null);
		// canvas.drawBitmap(chat_footer_right, 80 + length / 2,
		// mCanvasHeight / 2 - chat_middle.getHeight() / 2, null);
		// PiPoDesigner.drawCenterString(canvas, 80, mCanvasHeight / 2
		// - chat_middle.getHeight() / 2 + 10, b,
		// PiPoDesigner.COLOR_WHITE, PiPoDesigner.COLOR_PINK, 0,
		// b.length);
		// } else if (players[stt].getPosition() == Player.POSITION_3H) {
		// byte[] b = new byte[s.length()];
		// b = PiPoDesigner.toByteIndex(s);
		// int length = PiPoDesigner.getLengString(b,
		// PiPoDesigner.COLOR_WHITE, PiPoDesigner.COLOR_PINK, 0,
		// b.length);
		// for (int i = 0; i < length / 2; i++) {
		// canvas.drawBitmap(chat_middle, mCanvasWidth - 80 + i,
		// mCanvasHeight / 2 - chat_middle.getHeight() / 2,
		// null);
		// }
		// for (int i = 0; i < length / 2 + 5; i++) {
		// canvas.drawBitmap(chat_middle, mCanvasWidth - 80 - (i + 1),
		// mCanvasHeight / 2 - chat_middle.getHeight() / 2,
		// null);
		// }
		// canvas.drawBitmap(chat_header_right, mCanvasWidth - 80 - length
		// / 2 - chat_header_right.getWidth(), mCanvasHeight / 2
		// - chat_middle.getHeight() / 2, null);
		// canvas.drawBitmap(chat_footer_right, mCanvasWidth - 80 + length
		// / 2, mCanvasHeight / 2 - chat_middle.getHeight() / 2,
		// null);
		// PiPoDesigner.drawCenterString(canvas, mCanvasWidth - 80,
		// mCanvasHeight / 2 - chat_middle.getHeight() / 2 + 10,
		// b, PiPoDesigner.COLOR_WHITE, PiPoDesigner.COLOR_PINK,
		// 0, b.length);
		// } else if (players[stt].getPosition() == Player.POSITION_6H) {
		// byte[] b = new byte[s.length()];
		// b = PiPoDesigner.toByteIndex(s);
		// int length = PiPoDesigner.getLengString(b,
		// PiPoDesigner.COLOR_WHITE, PiPoDesigner.COLOR_PINK, 0,
		// b.length);
		// for (int i = 0; i < length / 2; i++) {
		// canvas.drawBitmap(chat_middle, mCanvasWidth / 2 + i,
		// mCanvasHeight - chat_middle.getHeight() - 5, null);
		// }
		// for (int i = 0; i < length / 2 + 5; i++) {
		// canvas.drawBitmap(chat_middle, mCanvasWidth / 2 - (i + 1),
		// mCanvasHeight - chat_middle.getHeight() - 5, null);
		// }
		// canvas.drawBitmap(chat_header_right, mCanvasWidth / 2 - length
		// / 2 - chat_header_right.getWidth(), mCanvasHeight
		// - chat_middle.getHeight() - 5, null);
		// canvas.drawBitmap(chat_footer_right, mCanvasWidth / 2 + length
		// / 2, mCanvasHeight - chat_middle.getHeight() - 5, null);
		// PiPoDesigner.drawCenterString(canvas, mCanvasWidth / 2,
		// mCanvasHeight - 65, b, PiPoDesigner.COLOR_WHITE,
		// PiPoDesigner.COLOR_PINK, 0, b.length);
		// }
		// }

		private void drawMoneyEffect(Canvas canvas) {
			boolean draw = false;
			for (int i = 0; i < players.length; i++) {
				if (players[i].draw_effect) {
					long time = System.currentTimeMillis();
					if (time > last_effect + MS_PER_FRAME) {
						draw = true;
						String money = Integer.toString(players[i]
								.getMoneyEffect());
						if (players[i].getMoneyEffect() > 0) {
							money = "+" + money;
						}
						byte[] b = new byte[money.length()];
						b = PiPoDesigner.toByteIndex(money);
						if (players[i].getPosition() == Player.POSITION_12H) {
							PiPoDesigner.drawCenterString(canvas,
									mCanvasWidth / 2, mCanvasHeight / 2 - 3
											* cardHeight / 2 - d, b, 0,
									PiPoDesigner.COLOR_YELOW, 0, b.length);
						} else if (players[i].getPosition() == Player.POSITION_3H) {
							PiPoDesigner.drawCenterString(canvas, mCanvasWidth
									/ 2 + cardWidth, mCanvasHeight / 2 - d, b,
									0, PiPoDesigner.COLOR_YELOW, 0, b.length);
						} else if (players[i].getPosition() == Player.POSITION_6H) {
							PiPoDesigner.drawCenterString(canvas,
									mCanvasWidth / 2, mCanvasHeight / 2
											+ cardHeight / 2
											+ MAX_LENGTH_MONEY_EFFECT - d, b,
									0, PiPoDesigner.COLOR_YELOW, 0, b.length);
						} else if (players[i].getPosition() == Player.POSITION_9H) {
							PiPoDesigner.drawCenterString(canvas, mCanvasWidth
									/ 2 - cardWidth, mCanvasHeight / 2 - d, b,
									0, PiPoDesigner.COLOR_YELOW, 0, b.length);
						}

					}
				}
			}
			d++;
			if (d > MAX_LENGTH_MONEY_EFFECT) {
				draw_money_effect = false;
				d = 0;
				for (int j = 0; j < players.length; j++)
					players[j].draw_effect = false;
			}
			if (draw)
				last_effect = time;

		}

		private int clock() {
			long now = System.currentTimeMillis();
			if (now > lastTime + 1000) {
				time--;
				lastTime = System.currentTimeMillis();

				if (playerActivted.elementAt(luotDanh) == getSttMe()) {
					if (time == 5) {
						if (display_boc) {
							display_boc = false;
							display_an = false;
							if (update_button == UPDATE_BUTTON0
									|| update_button == UPDATE_BUTTON1
									|| update_button == UPDATE_BUTTON2)
								display_danh = true;
							else if (update_button == UPDATE_BUTTON3)
								display_ha_phom = true;
							((PhomActivity) mContext).sendNBRequest();
						}
					}

					if (time == 2) {
						if (display_ha_phom) {
							display_danh = true;
							display_ha_phom = false;
							start_ha_phom = true;
						}
					}

					if (time == 0) {
						if (display_danh) {
							display_danh = false;
							stt_db = card6h.size() - 1;

							setDanhBai();

							setTime();
						}
					}
				}
				if (time < 0) {
					time = -1;
					drawClock = false;
				}
			}
			return time;
		}
	}

	private GameThread thread;

	public PhomView(Context context) {
		super(context);

		SurfaceHolder holder = getHolder();
		holder.addCallback(this);

		thread = new GameThread(holder, context);

		setFocusable(true); // make sure we get key events
		setFocusableInTouchMode(true); // make sure we get touch events
	}

	public GameThread getThread() {
		return thread;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		return thread.doKeyDown(keyCode, msg);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent msg) {
		return thread.doKeyUp(keyCode, msg);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return thread.doTouch(event);
	}

	// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	// thread.setShowEditText(false);
	// thread.setMessage("");
	// }

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	public void surfaceCreated(SurfaceHolder holder) {
		thread.setRunning(true);
		thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		thread.setRunning(false);
	}

}
